﻿@inherits BaseComponent

<canvas id="@Id" style="@Style"></canvas>

@code {
    //条形码组件类，配置参考barcode.js。
    private string lastCode;

    /// <summary>
    /// 取得或设置条形码组件样式。
    /// </summary>
    [Parameter] public string Style { get; set; }

    /// <summary>
    /// 取得或设置条形码组件条码值。
    /// </summary>
    [Parameter] public string Value { get; set; }

    /// <summary>
    /// 取得或设置条形码组件条码对象，参考Barcode.js配置。
    /// </summary>
    [Parameter] public object Option { get; set; }

    protected override void OnInitialized()
    {
        Id = $"bc-{Id}";
    }

    /// <summary>
    /// 条形码组件呈现后，调用Barcode.js绘制条形码。
    /// </summary>
    /// <param name="firstRender">是否首次呈现。</param>
    /// <returns></returns>
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender || Value != lastCode)
        {
            lastCode = Value;
            await JS.ShowBarcodeAsync(Id, Value, Option);
        }
    }
}